/*
 * 动态构建模态框插件
 * @author Li
 * @date 2017-3
 */
(function($) {
	createModal = function(opts) {
		opts = $.extend(modal.default_opts, opts || {});
		modal.create(opts);		
	};
	modal = {

		default_opts : {
			title : '普通模态对话框',	//	模态的title
			content:'未指定内容界面',	//	内容,若url错误,会呈现的内容
			url:'',					//	请求的加载内容的地址
			method:null,			//	请求方式,默认GET modal内容的请求方式
			tabid:'',				//	
			ajax:true,
			methodUrl:'',			//	提交url
			formid:'',			//	提交表单的id 
			modalid:'myModal'		//	模态id
			//submit_method:'POST'
		},
		create : function(opts) {
			 $("#"+opts.modalid).remove();
			//模态组件构建
			var $btn_close = $('<button>', {
				'class' : 'btn btn-default',
				'data-dismiss' : 'modal'
			}).text('关闭');
			var $btn_save = $('<button>', {
				'class' : 'btn btn-primary',
				'id':'modelSumbit',
				'method':opts.method,
				'data-dismiss':'modal',//点击后关闭								
				'url':opts.methodUrl,
				'data-addtab':opts.tabid,
				'ajax':opts.ajax,
				'formid':opts.formid,
				'submit_method':opts.submit_method
			}).text('确定');
			var $div_foot = $('<div>', {
				'class' : 'modal-footer'
			}).append($btn_close).append($btn_save);

			var $div_modalBody = $('<div>', {
				'class' : 'modal-body'
			})

			var $h4 = $('<h4>', {
				'class' : 'modal-title',
				'id' : 'myModalLabel'
			}).text(opts.title);
			var $span = $('<span>', {
				'aria-hidden' : 'true'
			}).html('&times');
			var $btn_close = $('<button>', {
				'class' : 'close',
				'data-dismiss' : 'modal',
				'aria-label' : 'Close'
			}).append($span);
			var $div_modalHeader = $('<div>', {
				'class' : 'modal-header'
			}).append($btn_close).append($h4);

			var $div_modalContent = $('<div>', {
				'class' : 'modal-content'
			}).append($div_modalHeader).append($div_modalBody)
					.append($div_foot);

			var $div_modalDialog = $('<div>', {
				'class' : "modal-dialog modal-lg",
				'role' : 'document'
			}).append($div_modalContent);

			var $div_modal = $("<div>", {
				'class' : "modal fade bs-example-modal-lg",
				'id' : opts.modalid,
				'tabindex' : "-1",
				'role' : "dialog",
				'aria-labelledby' : "myModalLabel"
			}).append($div_modalDialog);
			//  模态内容请求
			console.log(opts.url);
			if (opts.url == "") {
				$div_modalBody.append(opts.content);
			} else {
				$.ajax({
					url : opts.url,
					method : opts.method
				}).done(function(results) {
					// console.log(results);
					$div_modalBody.append(results);
				});
			}
			// 总填充
			$("body").append($div_modal);
		}
	}
})(jQuery)